package br.com.luiz.calculadora.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.rpc.ServiceException;
import javax.xml.ws.BindingProvider;

import br.com.luiz.calculadora.server.Calculadora;
import br.com.luiz.calculadora.server.CalculadoraImplServiceLocator;

@WebServlet("/calculadoraH")
public class CalculadoraHistoricoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		Long valor1 = Long.valueOf(request.getParameter("v1"));
		Long valor2 = Long.valueOf(request.getParameter("v2"));

		
		CalculadoraImplServiceLocator proxy = new CalculadoraImplServiceLocator();
		
		long resultado = 0L;
		try {
			Calculadora port = proxy.getCalculadoraImplPort();
		
			((BindingProvider)port).getRequestContext().put(BindingProvider.SESSION_MAINTAIN_PROPERTY, true);
		
			resultado = port.somaHistorico(valor1, valor2);
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		request.setAttribute("resultado", resultado);
		
		HttpSession sessao = request.getSession();
		sessao.setAttribute("ultimoResultado", resultado);

		request.getRequestDispatcher("resultadoH.jsp")
				.forward(request, response);
	}

}
